﻿using System.Collections.Generic;
using System.Linq;
using RESTController.Lib;

namespace RESTController
{
    public class PagedList<T> : List<T>
    {
        public PagedList(IEnumerable<T> collection, int pageIndex, int pageSize)
        {
            Check.ArgumentNotNull(collection, "collection");
            Check.ArgumentFollowsRule(pageIndex, i => i >= 0, "Page index must be greater than or equal to 0.");
            Check.ArgumentFollowsRule(pageSize, i => i > 0, "Page size must be greate than 0.");

            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = collection.Count();

            TotalPages = PageCountCalculator.CalculateNumberOfPages(PageSize, TotalCount);
            AddRange(collection.Skip(PageIndex * PageSize).Take(PageSize));
        }

        public int CurrentPage
        {
            get { return PageIndex + 1; }
        }
        public bool HasNextPage
        {
            get { return CurrentPage < TotalPages; }
        }
        public bool HasPreviousPage
        {
            get { return CurrentPage > 1; }
        }
        public int NextPage
        {
            get { return CurrentPage + 1; }
        }
        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int PreviousPage
        {
            get { return CurrentPage - 1; }
        }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }
    }
}
